﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Doo.WebUI.core.js"/>
/// <reference name="Doo.WebUI.VersionManager.script.js"/>

Type.registerNamespace("Doo.DataSource");

Doo.Parameter = function () {
    Doo.Parameter.initializeBase(this);
}
Doo.Parameter.prototype =
{
    name: '',
    defaultValue: '',
    get_value: function () { return this.defaultValue; }
}

Doo.Parameter.registerClass('Doo.Parameter', Sys.Component);

Doo.ParameterQueryString = function () { }
Doo.ParameterQueryString.prototype =
{
    queryName: '',
    get_value: function () {
        return window.location.search;
    }
}
Doo.ParameterQueryString.registerClass('Doo.ParameterQueryString', Doo.Parameter);

Doo.ParameterCookie = function () { }
Doo.ParameterCookie.prototype =
{
    cookieName: '',
    get_value: function () { }
}
Doo.ParameterCookie.registerClass('Doo.ParameterCookie', Doo.Parameter);

Doo.ParameterControl = function () { }
Doo.ParameterControl.prototype =
{
    controlid: '',
    propname: 'value',
    get_value: function (scope) {
        var ctl;
        if (scope) {
            ctl = scope.findChild(this.controlid);
        }
        if (!ctl) {
            var elem = $get(this.controlid);
            if (!elem) throw new Error("找不到元素");
            ctl = elem.control || elem;
        }

        //寻找属性
        var propname = this.propname;
        var getmethod = 'get_' + propname;
        if (typeof (ctl[getmethod]) == 'function') {
            return ctl[getmethod]();
        }
        if (propname in ctl) {
            return ctl[propname];
        }

        throw new Error("不存在的属性或字段");
    }
}
Doo.ParameterControl.registerClass('Doo.ParameterControl', Doo.Parameter);

Doo.DataSource.Base = function (element) {
    Doo.DataSource.Base.initializeBase(this, [element]);
}

Doo.DataSource.Base.prototype = {
    initialize: function () {
        Doo.DataSource.Base.callBaseMethod(this, 'initialize');
        // Add custom initialization here
    },
    dispose: function () {
        //Add custom dispose actions here
        Doo.DataSource.Base.callBaseMethod(this, 'dispose');
    },

    /* 缓存相关 */
    enableversion: false,
    versionkey: '',
    get_version: function () {
        return Doo.Caching.DataVersionManager.get_version(this.versionkey);
    },

    /* select方法 */
    select: function (onsucc, onfail, option, userContext) {
        ///<summary>选择接口</summary>
        if (this.onselecting) this.onselecting(this);
    },
    selectsucc: function (result, args) {
        //result:数据，args:select方法的参数

        if (this.onselected) this.onselected(this);

        var onsucc = args.onsucc;
        if (!onsucc) return;
        onsucc(result, args.userContext);
    },
    selectfail: function (err, args) {

        if (this.onselected) this.onselected(this);

        var onfail = args && args.onfail;
        if (!onfail) alert(err.get_message());
        else onfail(err, args.userContext);
    },

    initevents: { selecting: 0, selected: 0 }

}
Doo.DataSource.Base.registerClass('Doo.DataSource.Base', Doo.ControlBase);

Doo.DataSource.Base.prototype.Select = Doo.DataSource.Base.prototype.select;
